﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.AppVersion;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.EventAdvertising;

namespace FwProject.Manager
{
    public class ShopCouponManager : IDomainService
    {
        private readonly IRepository<ShopCoupon, Guid> _ShopCouponRepository;
        private readonly IRepository<ShopUserCoupon, Guid> _ShopUserCouponRepository;

        public ShopCouponManager(IRepository<ShopCoupon, Guid> ShopCouponRepository, IRepository<ShopUserCoupon, Guid> ShopUserCouponRepository)
        {
            _ShopCouponRepository = ShopCouponRepository;
            _ShopUserCouponRepository = ShopUserCouponRepository;
        }
        /// <summary>
        /// 添加一个优惠券
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> CreateShopCoupon(ShopCoupon model)
        {
            return await _ShopCouponRepository.InsertAndGetIdAsync(model);
        }
        /// <summary>
        /// 修改一个优惠券
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task UpdateShopCoupon(ShopCoupon model)
        {
            await _ShopCouponRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 删除一个优惠券
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task DeleteShopCoupon(ShopCoupon model)
        {
            if (_ShopUserCouponRepository.GetAll().Where(o => o.ShopCouponId == model.Id).Count() > 0)
                throw new UserFriendlyException("优惠券已派发到用户手中不可删除");

            await _ShopCouponRepository.DeleteAsync(model);
        }
    }
}
